********************************************************************************
** 	TITLE:		ON11_results                                                  ** 	
**	AUTHOR:	    Philippe Mongrain                                             **
**	DATA:       ON11_results                                                  **
**	VERSION:	Stata 16					                                  **
**	DATE:		October 2022  				                                  **
********************************************************************************

* Version control

version 16.0

* 1. Download the "Valid Votes Cast for Each Candidate" file from this link: https://results.elections.on.ca/api/report-groups/3/report-outputs/499/csv  
* 2. Save the file to the CSV format with the following name: "ON11_results.csv"

* Import the dataset

import delimited "ON11_results.csv"

* Keep only general election

drop if isgeneralelection == 0

* Rename and (re)create variables

rename electoraldistrictnumber districtnumber11
rename electoraldistrictnameenglish districtname11
rename politicalinterestcode partyname11
rename totalvalidballotscast numberofvotes11
gen numberofvotespercent11 = percentoftotalvalidballotscast*100

* Generate dichotomous variable for each party

gen lib11 = 1 if partyname11 == "L"
replace lib11 = 0 if lib11!=1

gen con11 = 1 if partyname11 == "PC"
replace con11 = 0 if con11!=1

gen ndp11 = 1 if partyname11 == "ND"
replace ndp11 = 0 if ndp11!=1

gen green11 = 1 if partyname11 == "GP"
replace green11 = 0 if green11!=1

* Generate party variable

gen party11 = 1 if lib11 == 1
replace party11 = 2 if con11 == 1
replace party11 = 3 if ndp11 == 1
replace party11 = 4 if green11 == 1
replace party11 = 88 if party11 == . 

* Generate rank of parties

gsort districtname11 -numberofvotes11
bysort districtname11 : gen rank11 = _n

gen first11 = party11 if rank11 == 1
gen second11 = party11 if rank11 == 2
gen third11 = party11 if rank11 == 3

bysort districtname11 : gen winner11 = first11[1]
bysort districtname11 : gen runnerup11 = second11[2]
bysort districtname11 : gen thirdplace11 = third11[3]

* Generate margins of victory

bysort districtname11 : gen margin11 = numberofvotespercent11[1] - numberofvotespercent11[2]

* Generate effective number of electoral parties

gen sqrvotepr11 = (numberofvotespercent11/100)^2

bysort districtname11 : egen sumsqrvotepr11 = sum(sqrvotepr11)

gen enep11 = 1/sumsqrvotepr11

* Labelling parties

label define party 1 "Liberal Party" 2 "Progressive Conservative Party" 3 "New Democratic Party" 4 "Green Party" 88 "Other"
label values winner11 runnerup11 thirdplace11 party

* Selecting, ordering, and sorting variables

keep districtnumber11 districtname11 party11 rank11 numberofvotes11 numberofvotespercent11 margin11 enep11 winner11 runnerup11 thirdplace11

order districtnumber11 districtname11 party11 rank11 numberofvotes11 numberofvotespercent11 margin11 enep11 winner11 runnerup11 thirdplace11

sort districtnumber11 rank11

drop if rank11 > 3

* Drop duplicates

duplicates tag districtnumber11, gen(dup)
duplicates drop districtnumber11, force
drop dup

* Create election variable

gen election11 = "2011 Ontario general election"

* Saving file

save ON11_results, replace

* Merge with ON11_results.dta and generate reelection variable

merge 1:1 _n using ON07_results.dta

* Generate variable for incumbent party reelection

gen reelected11 = 1 if winner11 == winner07
replace reelected11 = 0 if reelected11!=1

* Saving file

save ON0711_results, replace